package com.bing.jh.controller;

import com.bing.bingPro.common.BaseResult;
import com.bing.bingPro.util.ExcelData;
import com.bing.bingPro.util.WebExcelUtil;
import com.bing.jh.entity.dto.TTfrDetailDayDTO;
import com.bing.jh.entity.vo.TTfrDetailDayVO;
import com.bing.jh.entity.vo.TTfrDetailReasonVO;
import com.bing.jh.service.TTfrDetailDayService;

import com.github.pagehelper.PageInfo;
import com.github.pagehelper.page.PageMethod;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * 故障终端统计
 */
@RestController
@RequestMapping("tTfrDetailDay")
public class TTfrDetailDayController {

    @Resource
    private TTfrDetailDayService detailDayService;

    @PostMapping("/list")
    public BaseResult<PageInfo<TTfrDetailDayVO>> list(@RequestBody TTfrDetailDayDTO tTfrDetailDayDTO) {
        PageMethod.startPage(tTfrDetailDayDTO.getCurrent(), tTfrDetailDayDTO.getSize());
        List<TTfrDetailDayVO> list = detailDayService.list(tTfrDetailDayDTO);

        // 获取分页信息
        PageInfo<TTfrDetailDayVO> pageInfo = new PageInfo<>(list);

        // 返回包装后的分页结果
        return new BaseResult<>(pageInfo);
    }



    @PostMapping("/export")
    public void export(HttpServletResponse response, @RequestBody TTfrDetailDayDTO tTfrDetailDayDTO) throws Exception {
        List<TTfrDetailDayVO> list = detailDayService.list(tTfrDetailDayDTO);
        //整理查询的数据
        List<ExcelData> excelDataList = new ArrayList<>();
        ExcelData excelData = new ExcelData(TTfrDetailDayVO.class,
                list);
        excelDataList.add(excelData);
        List<String> time = tTfrDetailDayDTO.getTime();
        String name = null;
        if (time.size() == 1) {
            name = time.get(0);
        } else if (time.size() > 1) {
            name = time.get(0) +"-"+ time.get(time.size() - 1);
        }
        //写出到浏览器
        WebExcelUtil.download(response, "故障终端年度统计表" +name , excelDataList);
    }





    @PostMapping("/dateList")
    public BaseResult<List<String>> dateList() {
        return new BaseResult<>(detailDayService.dateList());
    }



    @PostMapping("/reasonDetail")
    public BaseResult<PageInfo<TTfrDetailReasonVO>> queryReasonDetail(@RequestBody TTfrDetailDayDTO tTfrDetailDayDTO) {
        PageMethod.startPage(tTfrDetailDayDTO.getCurrent(), tTfrDetailDayDTO.getSize());
        List<TTfrDetailReasonVO> list = detailDayService.queryReasonDetail(tTfrDetailDayDTO);

        // 获取分页信息
        PageInfo<TTfrDetailReasonVO> pageInfo = new PageInfo<>(list);

        // 返回包装后的分页结果
        return new BaseResult<>(pageInfo);
    }



    @PostMapping("/exportReason")
    public void exportReason(HttpServletResponse response, @RequestBody TTfrDetailDayDTO tTfrDetailDayDTO) throws Exception {
        List<TTfrDetailReasonVO> list = detailDayService.queryReasonDetail(tTfrDetailDayDTO);
        list.forEach(tTfrDetailReasonVO -> {
            String causeReasonRankStr = tTfrDetailReasonVO.getCauseReasonRank();
            tTfrDetailReasonVO.setCauseReasonRank(String.format("%.2f%%",  Double.parseDouble(causeReasonRankStr) * 100));
        });
        //整理查询的数据
        List<ExcelData> excelDataList = new ArrayList<>();
        ExcelData excelData = new ExcelData(TTfrDetailReasonVO.class,
                list);
        excelDataList.add(excelData);
        List<String> time = tTfrDetailDayDTO.getTime();
        String name = null;
        if (time.size() == 1) {
            name = time.get(0);
        } else if (time.size() > 1) {
            name = time.get(0) +"-"+ time.get(time.size() - 1);
        }
        System.out.println("故障原因统计表"+name);
        //写出到浏览器
        WebExcelUtil.download(response, "故障原因统计表" +name , excelDataList);
    }


    @PostMapping("/detailDate")
    public BaseResult<List<String>> detailDate() {
        return new BaseResult<>(detailDayService.detailDate());
    }
}
